package edu.buffalo.cse.cse486_586.service;

import java.util.Comparator;
import edu.buffalo.cse.cse486_586.entity.PendingMessage;
import edu.buffalo.cse.cse486_586.util.AppUtil;

public class DeliveredMessageComparator implements Comparator<PendingMessage> {

	@Override
	public int compare(PendingMessage m1, PendingMessage m2) {
		if ( m1.getStatus() != AppUtil.DELIVERABLE || m2.getStatus() != AppUtil.DELIVERABLE ) {
			if (m1.getStatus() != AppUtil.DELIVERABLE && m2.getStatus() == AppUtil.DELIVERABLE) {
				return 1;
			} else if (m1.getStatus() == AppUtil.DELIVERABLE && m1.getStatus() != AppUtil.DELIVERABLE) {
				return -1;
			} else {
				return 0;
			}
		} else {
			if (m1.getFinalVm().getPurposedSequence()
					.equals(m2.getFinalVm().getPurposedSequence())) {

				double squ1 = Double.valueOf(m1.getFinalVm()
						.getPurposedSequence()
						+ "."
						+ m1.getFinalVm().getPurposedSequence());
				double squ2 = Double.valueOf(m2.getFinalVm()
						.getPurposedSequence()
						+ "."
						+ m2.getFinalVm().getPurposedSequence());
				return squ1 > squ2 ? 1 : -1;
			}else{
				int squ1 = Integer.valueOf(m1.getFinalVm()
						.getPurposedSequence());
				int squ2 = Integer.valueOf(m2.getFinalVm()
						.getPurposedSequence());
				return squ1 > squ2 ? 1 : -1;
			}
		}
	}
}
